{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#!/usr/bin/env python\n",
    "# coding: utf-8\n",
    "\n",
    "# In[22]:\n",
    "\n",
    "\n",
    "#!/usr/bin/env python\n",
    "# -*- coding: utf-8 -*-\n",
    "#\n",
    "# Copyright (c) 2020 Taste all Pi.\n",
    "#\n",
    "# Licensed under the GNU General Public License, Version 2.0 (the \"License\");\n",
    "# you may not use this file except in compliance with the License.\n",
    "# You may obtain a copy of the License at\n",
    "#\n",
    "#   http://www.gnu.org/licenses/gpl-2.0.html\n",
    "#\n",
    "# Unless required by applicable law or agreed to in writing, software\n",
    "# distributed under the License is distributed on an \"AS IS\" BASIS,\n",
    "# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n",
    "# See the License for the specific language governing permissions and\n",
    "# limitations under the License.\n",
    "\n",
    "#########  百度图像处理库使用说明  #################\n",
    "# baidu_picture_2_msg：初始化方法和获取图像信息\n",
    "# init初始化方法：\n",
    "#        需要传入百度AI应用提供的api_key和secret_key\n",
    "# pic_2_msg获取图像方法： \n",
    "#        对输入的图片进行人工智能分析，并获取分析结果\n",
    "#################################################\n",
    "\n",
    "#########  调用方式举例  #################\n",
    "# #导入标准库\n",
    "# import sys\n",
    "# from playsound import playsound\n",
    "# import cv2 as cv\n",
    "\n",
    "# #导入自定义库\n",
    "# sys.path.append('../baidu_api_lib')\n",
    "# from baidu_picture import baidu_picture_2_msg\n",
    "# \n",
    "# #传入参数\n",
    "# baidu_request_url = \"https://aip.baidubce.com/rest/2.0/image-classify/v1/body_analysis\"\n",
    "# baidu_api_key = 'EtVQHk3Oq3ZQDfNzGbW7sZLd'\n",
    "# baidu_api_secret = 'OjFeIol5wRCGRyjrk4YRUM07BN1qomBk'\n",
    "# \n",
    "# if __name__ == \"__main__\":\n",
    "#     try:\n",
    "#         # 打开摄像头\n",
    "#         capture = cv.VideoCapture(0) \n",
    "#         \n",
    "#         # 传入百度AI的参数\n",
    "#         pic_msg = baidu_picture_2_msg(baidu_api_id, baidu_api_key, baidu_api_secret)\n",
    "#         \n",
    "#         while True:\n",
    "#             # 一帧一帧读取视频\n",
    "#             ret, frame = capture.read()\n",
    "#             \n",
    "#             # 写入图片\n",
    "#             cv.imwrite('camera_pic.jpg',frame)  \n",
    "#             #time.sleep(1)\n",
    "#             \n",
    "#             response = pic_msg.pic_2_msg(baidu_request_url, 'camera_pic.jpg')\n",
    "#             print(response.json())\n",
    "#             \n",
    "#             # 本地显示视频图像\n",
    "#             cv.imshow('screen', frame) \n",
    "#             cv.waitKey(1)  \n",
    "#                \n",
    "#     except KeyboardInterrupt:             \n",
    "#         # 释放cap,销毁窗口\n",
    "#         capture.release()                                   \n",
    "#         cv.destroyAllWindows()                                  \n",
    "#         print(\"任务被终止\")\n",
    "#################################################      \n",
    "\n",
    "from aip import AipSpeech\n",
    "import requests\n",
    "import base64\n",
    "import re\n",
    "\n",
    "class baidu_picture_2_msg(object):\n",
    "    \n",
    "    #百度AI应用提供参数\n",
    "    REQUEST_URL = None\n",
    "    API_ID      = None\n",
    "    API_KEY     = None\n",
    "    SECRET_KEY  = None\n",
    "    response    = None\n",
    "    \n",
    "    def __init__(self, api_id, api_key, secret_key):\n",
    "        #获取提供的百度AI接口参数\n",
    "        self.API_ID = api_id\n",
    "        self.API_KEY = api_key\n",
    "        self.SECRET_KEY = secret_key\n",
    "        \n",
    "        #获取access_token值\n",
    "        main_host = 'https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id='\n",
    "        host = main_host + api_key + '&client_secret=' + secret_key\n",
    "        self.response = requests.get(host)\n",
    "        \n",
    "    def pic_2_msg(self, request_url, pic_name):\n",
    "        #获取应用的调用接口\n",
    "        self.REQUEST_URL = request_url \n",
    "        \n",
    "        if self.response:\n",
    "            #打开待处理的图片\n",
    "            f = open(pic_name, 'rb')\n",
    "            img = base64.b64encode(f.read())\n",
    "            params = {\"image\":img}\n",
    "\n",
    "            #获取access_token值\n",
    "            access_token = self.response.json()['access_token']\n",
    "            self.REQUEST_URL = self.REQUEST_URL + \"?access_token=\" + access_token\n",
    "            headers = {'content-type': 'application/x-www-form-urlencoded'}\n",
    "\n",
    "            #获得请求后，进行信息输出\n",
    "            response = requests.post(self.REQUEST_URL, data=params, headers=headers)\n",
    "            if response:\n",
    "                return response\n",
    "        return None"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.5.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
